package spring.superrun.record.mvc.controller;

import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import spring.superrun.record.mvc.dto.UserInfoAddDto;
import spring.superrun.record.mvc.dto.UserInfoUpdateDto;
import spring.superrun.record.mvc.entity.SuperrunUserinfos;
import spring.superrun.record.mvc.entity.enumlibs.ServiceResultCode;
import spring.superrun.record.mvc.entity.response.ServiceResult;
import spring.superrun.record.mvc.entity.response.ServiceResultOfT;
import spring.superrun.record.mvc.service.SuperrunUserinfosService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 微信用户管理接口
 *
 * @author makejava
 * @since 2021-06-18 08:33:26
 */
@RestController
@RequestMapping("api/wechat_users")
public class SuperrunUserinfosController {
    /**
     * 服务对象
     */
    @Resource
    private SuperrunUserinfosService superrunUserinfosService;

    /**
     * 通过主键查询单条数据
     * @return 单条数据
     */
    @GetMapping
    @ApiOperation("获取所有微信用户")
    public ResponseEntity<ServiceResultOfT<List<SuperrunUserinfos>>> GetUserInfos(){
        ServiceResultOfT<List<SuperrunUserinfos>> result = new ServiceResultOfT<List<SuperrunUserinfos>>();
        result.IsFailed(ServiceResultCode.NotFound,"未找到符合条件的微信用户信息");
        List<SuperrunUserinfos> entities =superrunUserinfosService
                .queryAllByLimit(0,100);
//        entities.sort((o1,o2)->o2.getOrderid().compareTo(o1.getOrderid()));
        result.setEntities(entities);
        if (result.getEntities()==null){
            return new ResponseEntity<>(result, HttpStatus.NOT_FOUND);
        }
        result.IsSuccess("成功获取到所有微信用户");
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     *通过id查询一个微信用户信息
     * @return ResponseEntity<ServiceResultOfT<SuperrunUserinfos>>
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取某个微信用户信息根据id")
    public  ResponseEntity<ServiceResultOfT<SuperrunUserinfos>> GetUserInfo(@PathVariable String id){
        ServiceResultOfT<SuperrunUserinfos> result = new ServiceResultOfT<>();
        result.IsFailed(ServiceResultCode.NotFound,"未找到符合条件的微信用户信息");
        if (id==null||id.isEmpty()){
            result.IsFailed(ServiceResultCode.KeyIsNull,"为输入关键信息");
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        result.setEntities(superrunUserinfosService.queryById(id));
        if (result.getEntities()==null){
            return new ResponseEntity<>(result,HttpStatus.NOT_FOUND);
        }
        result.IsSuccess("查询一个微信用户信息成功");
        return  new ResponseEntity<>(result,HttpStatus.OK);
    }
    @PostMapping
    @ApiOperation("添加一个微信用户信息")
    public ResponseEntity<ServiceResult> AddUserInfo(@RequestBody @Valid UserInfoAddDto dto){
        ServiceResult result = new ServiceResult();
        result.IsFailed("添加失败");
        if (dto==null){
            result.IsFailed(ServiceResultCode.KeyIsNull,"微信用户关键信息为空,添加失败");
            return new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        SuperrunUserinfos UserInfo = new SuperrunUserinfos();
        SuperrunUserinfos UserInfoAdd = superrunUserinfosService.insert(UserInfo.Add(dto));
        if (UserInfoAdd==null){
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        result.IsSuccess("添加一个微信用户信息成功");
        return  new ResponseEntity<>(result,HttpStatus.OK);
    }
    @PutMapping
    @ApiOperation("更新一个微信用户信息")
    public ResponseEntity<ServiceResult> UpdateUserInfo(@RequestBody @Valid UserInfoUpdateDto dto){
        ServiceResult result = new ServiceResult();
        result.IsFailed("更新失败");
        if (dto.getId()==null||dto.getId().isEmpty()){
            result.IsFailed(ServiceResultCode.KeyIsNull,"为输入关键信息");
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        SuperrunUserinfos UserInfo = superrunUserinfosService.queryById(dto.getId());
        if (UserInfo==null){
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        SuperrunUserinfos UserInfoUpdate= superrunUserinfosService.update(UserInfo.Update(dto));
        if (UserInfoUpdate==null){
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        result.IsSuccess("更新成功");
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
    @DeleteMapping(value = "/{id}")
    @ApiOperation("删除一个微信用户信息")
    public ResponseEntity<ServiceResult> DelUserInfo
            (@PathVariable @Length(min = 20,max = 50) String id){
        ServiceResult result = new ServiceResult();
        result.IsFailed("删除失败");
        if (id==null||id.isEmpty()){
            result.IsFailed(ServiceResultCode.KeyIsNull,"为输入关键信息");
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
//        boolean isDel = superrunUserinfosService.deleteById(id);
//        if (!isDel){
//            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
//        }
        result.IsSuccess("删除成功");
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
}
